/**
 * ShieldPowerUp - 护盾道具
 * 让玩家免疫一次碰撞伤害
 */

import { PowerUp, PowerUpType, PowerUpRarity, type PowerUpConfig, type PowerUpEffect, type PowerUpOptions } from '../PowerUp'
import type { Player } from '../Player'

/**
 * 护盾效果实现
 */
export class ShieldEffect implements PowerUpEffect {
  private shieldActive: boolean = false
  private originalTakeDamage: ((damage: number) => void) | null = null

  /**
   * 应用护盾效果
   */
  apply(player: Player): void {
    this.shieldActive = true
    
    // 保存原始的受伤方法
    this.originalTakeDamage = player.takeDamage.bind(player)
    
    // 重写受伤方法以实现护盾效果
    player.takeDamage = (damage: number) => {
      if (this.shieldActive) {
        console.log('护盾抵挡了一次伤害！')
        this.shieldActive = false
        // 护盾被消耗，但不受伤害
        return
      }
      
      // 护盾已被消耗或不存在，正常受伤
      if (this.originalTakeDamage) {
        this.originalTakeDamage(damage)
      }
    }

    console.log('护盾效果已激活，可以抵挡一次伤害')
  }

  /**
   * 移除护盾效果
   */
  remove(player: Player): void {
    // 恢复原始的受伤方法
    if (this.originalTakeDamage) {
      player.takeDamage = this.originalTakeDamage
      this.originalTakeDamage = null
    }
    
    this.shieldActive = false
    console.log('护盾效果已结束')
  }

  /**
   * 更新护盾效果
   */
  update(_player: Player, _deltaTime: number): void {
    // 护盾效果通常不需要每帧更新
    // 可以在这里添加护盾的视觉效果
  }

  /**
   * 检查护盾是否激活
   */
  isShieldActive(): boolean {
    return this.shieldActive
  }

  /**
   * 手动消耗护盾
   */
  consumeShield(): void {
    this.shieldActive = false
  }
}

/**
 * 护盾道具类
 */
export class ShieldPowerUp extends PowerUp {
  constructor(options: Omit<PowerUpOptions, 'powerUpType'>) {
    super({
      ...options,
      powerUpType: PowerUpType.SHIELD
    })
  }

  /**
   * 创建护盾道具配置
   */
  protected createConfig(customConfig?: Partial<PowerUpConfig>): PowerUpConfig {
    const defaultConfig: PowerUpConfig = {
      type: PowerUpType.SHIELD,
      duration: 0, // 护盾是一次性的，不基于时间
      value: 1, // 可以抵挡的伤害次数
      rarity: PowerUpRarity.RARE,
      stackable: true,
      maxStacks: 3,
      description: '抵挡一次碰撞伤害',
      iconColor: '#4CAF50' // 绿色
    }

    return { ...defaultConfig, ...customConfig }
  }

  /**
   * 创建护盾效果
   */
  protected createDefaultEffect(): PowerUpEffect {
    return new ShieldEffect()
  }

  /**
   * 渲染护盾道具的特殊效果
   */
  protected renderDefault(ctx: CanvasRenderingContext2D): void {
    // 绘制盾牌形状
    ctx.fillStyle = this.config.iconColor
    
    const centerX = 0
    const centerY = 0
    const size = Math.min(this.width, this.height) * 0.8
    
    // 绘制盾牌主体
    ctx.beginPath()
    ctx.moveTo(centerX, centerY - size/2)
    ctx.quadraticCurveTo(centerX + size/2, centerY - size/3, centerX + size/2, centerY)
    ctx.quadraticCurveTo(centerX + size/2, centerY + size/3, centerX, centerY + size/2)
    ctx.quadraticCurveTo(centerX - size/2, centerY + size/3, centerX - size/2, centerY)
    ctx.quadraticCurveTo(centerX - size/2, centerY - size/3, centerX, centerY - size/2)
    ctx.closePath()
    ctx.fill()

    // 绘制盾牌装饰
    ctx.strokeStyle = '#66BB6A'
    ctx.lineWidth = 2
    
    // 十字装饰
    ctx.beginPath()
    ctx.moveTo(centerX, centerY - size/3)
    ctx.lineTo(centerX, centerY + size/3)
    ctx.moveTo(centerX - size/3, centerY)
    ctx.lineTo(centerX + size/3, centerY)
    ctx.stroke()

    // 绘制护盾光环
    ctx.strokeStyle = '#81C784'
    ctx.lineWidth = 1
    ctx.setLineDash([2, 2])
    ctx.beginPath()
    ctx.arc(centerX, centerY, size/2 + 4, 0, Math.PI * 2)
    ctx.stroke()
    ctx.setLineDash([]) // 重置虚线
  }
}